package controllers.validation.node;

import org.junit.Assert;
import org.junit.Test;

import controllers.validation.AbstractValidationTest;
import controllers.validation.jobs.NodeValidationJob;
import controllers.validation.jobs.SensorValidationJob;
import ch.bfh.abcm.rems.gateway.GateWayMessageType;
import ch.bfh.abcm.rems.gateway.GatewayMessage;
import ch.bfh.abcm.rems.sensors.tinkerforge.TFSensorType;
import ch.bfh.abcm.rems.tinkerforge.TFNodeAverageMessage;

public class SystemOkayNodeValidationJob extends AbstractValidationTest {

    @Test
    public void testEffectiveValueAndSystemOkayTemperature() throws InterruptedException {
        insertCorrectTempLuxHumSensorData(5);
        insertCorrectNodeAverages(5);
        new NodeValidationJob(HOST_IP_TEMPERATURE).now();
        Thread.sleep(WAIT_FOR_RECEIVING);
        Assert.assertNull(receiverimplemntation.getUpdateNodeConfigMessage());
        Assert.assertNull(receiverimplemntation.getUpdateUserConfigMessage());
        Assert.assertNull(receiverimplemntation.getTfNodeAverageMessage());
        Assert.assertNotNull(receiverimplemntation.getGatewayMessage());
        GatewayMessage gatewayMessage = receiverimplemntation.getGatewayMessage();
        Assert.assertEquals(GateWayMessageType.SYSTEM_OK, gatewayMessage.getMessageType());
        Assert.assertEquals(gatewayMessage.getMessage(),
                mtc.getMesurementPointOKMessage(HOST_IP_TEMPERATURE, TFSensorType.TEMPERATURE, 20));
    }

    @Test
    public void testEffectiveValueAndSystemOkayAmbient() throws InterruptedException {
        insertCorrectTempLuxHumSensorData(5);
        insertCorrectNodeAverages(5);
        new NodeValidationJob(HOST_IP_AMBIENT).now();
        Thread.sleep(WAIT_FOR_RECEIVING);
        Assert.assertNull(receiverimplemntation.getUpdateNodeConfigMessage());
        Assert.assertNull(receiverimplemntation.getUpdateUserConfigMessage());
        Assert.assertNull(receiverimplemntation.getTfNodeAverageMessage());
        Assert.assertNotNull(receiverimplemntation.getGatewayMessage());
        GatewayMessage gatewayMessage = receiverimplemntation.getGatewayMessage();
        Assert.assertEquals(GateWayMessageType.SYSTEM_OK, gatewayMessage.getMessageType());
        Assert.assertEquals(gatewayMessage.getMessage(),
                mtc.getMesurementPointOKMessage(HOST_IP_AMBIENT, TFSensorType.AMBIENT, 500));
    }

    @Test
    public void testEffectiveValueAndSystemOkayHumidity() throws InterruptedException {
        insertCorrectTempLuxHumSensorData(5);
        insertCorrectNodeAverages(5);
        new NodeValidationJob(HOST_IP_HUMIDITY).now();
        Thread.sleep(WAIT_FOR_RECEIVING);
        Assert.assertNull(receiverimplemntation.getUpdateNodeConfigMessage());
        Assert.assertNull(receiverimplemntation.getUpdateUserConfigMessage());
        Assert.assertNull(receiverimplemntation.getTfNodeAverageMessage());
        Assert.assertNotNull(receiverimplemntation.getGatewayMessage());
        GatewayMessage gatewayMessage = receiverimplemntation.getGatewayMessage();
        Assert.assertEquals(GateWayMessageType.SYSTEM_OK, gatewayMessage.getMessageType());
        Assert.assertEquals(gatewayMessage.getMessage(),
                mtc.getMesurementPointOKMessage(HOST_IP_HUMIDITY, TFSensorType.HUMIDITY, 50));
    }
}